package com.toilet.easyvideofx.handler;

import atlantafx.base.theme.PrimerLight;
import com.toilet.easyvideofx.ClientApplication;
import com.toilet.easyvideofx.global.GlobalCache;
import com.toilet.easyvideofx.constant.SystemConstant;
import com.toilet.easyvideofx.global.GlobalException;
import com.toilet.easyvideofx.theme.ThemeManager;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

/**
 * 初始化处理器
 */
public class InitializeHandler {
    /**
     * 此处初始化数据库以及登录功能
     */
    public void init(Stage stage) throws IOException {
        //设置明亮主体 todo 后续做成配置化
        PrimerLight light = new PrimerLight();
        //设置图标
        Image icon = new Image(Objects.requireNonNull(getClass().getResourceAsStream("/image/chat_icon.png")));
        stage.getIcons().add(icon);
        Application.setUserAgentStylesheet(light.getUserAgentStylesheet());

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/login-view.fxml"));
        //登录界面设定无法改变大小
        stage.setResizable(false);
        Scene scene = new Scene(fxmlLoader.load());
        //设置主窗口大小
        stage.setHeight(400);
        stage.setWidth(600);

        // 初始化主题管理器
        var tm = ThemeManager.getInstance();
        tm.setScene(scene);
        tm.setTheme(tm.getDefaultTheme());

        stage.setTitle("登录");
        stage.setScene(scene);
        stage.centerOnScreen();
        // 缓存stage
        GlobalCache.put(SystemConstant.MAIN_STAGE, stage);
        //初始化配置
        initConfig();

        System.out.println(GlobalCache.show());
        //初始化全局异常监听器
//        GlobalException exception = new GlobalException(stage);
        ClientApplication.shouMainStage(stage);
    }

    /**
     * 初始化配置文件
     *
     * @throws IOException
     */
    private void initConfig() throws IOException {
        // 此处读取配置文件到系统缓存容器中
        InputStream inputStream = InitializeHandler.class.getClassLoader().getResourceAsStream("config.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        // 缓存数据库地址
        putDbCache(properties);
        for (String propertyName : properties.stringPropertyNames()) {
            switch (propertyName) {
                //初始化请求地址
                case SystemConstant.BASE_REQUEST_URL ->
                        GlobalCache.put(SystemConstant.BASE_REQUEST_URL, properties.getProperty(propertyName));
                default -> System.out.println("暂无匹配");
            }
        }
    }

    private void putDbCache(Properties properties) {
        String osName = System.getProperty("os.name");
        if (osName.startsWith(SystemConstant.MAC)) {
            System.out.println("当前系统是Mac");
        } else if (osName.startsWith(SystemConstant.WINDOWS)) {
            System.out.println("当前系统是Windows");
        } else if (osName.startsWith(SystemConstant.LINUX)) {
            System.out.println("当前系统是Linux");
        } else {
            System.out.println("当前系统无法识别");
        }
    }

}